diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index f839654..7691dec 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -3575,13 +3575,38 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 		var cdecl = new CCodeDeclaration ("GRegex*");
 
 		var cname = regex_var.name + "regex_" + next_regex_id.to_string ();
+		if (this.next_regex_id == 0) {
+			var fun = new CCodeFunction ("_thread_safe_regex_init", "GRegex*");
+			fun.modifiers = CCodeModifiers.STATIC | CCodeModifiers.INLINE;
+			fun.add_parameter (new CCodeFormalParameter ("re", "GRegex**"));
+			fun.add_parameter (new CCodeFormalParameter ("pattern", "const gchar *"));
+			fun.add_parameter (new CCodeFormalParameter ("match_options", "GRegexMatchFlags"));
+/*
+ * The following should be appended to the source:
+ *
+
+static inline GRegex* _thread_safe_regex_init (GRegex** re, const gchar*
+pattern,
+GRegexMatchFlags match_options)
+{
+    if (g_once_init_enter ((volatile gsize*) re)) {
+        GRegex* val = g_regex_new (pattern, match_options, 0, NULL);
+        g_once_init_leave ((volatile gsize *) re, (gsize) val);
+    }
+
+    return *re;
+}
+
+*/
+
+			source_declarations.add_type_member_declaration (fun);
+		}
 		this.next_regex_id++;
 
 		cdecl.add_declarator (new CCodeVariableDeclarator (cname + " = NULL"));
 		cdecl.modifiers = CCodeModifiers.STATIC;
 
-		var regex_const = new CCodeConstant ("(%s == NULL) ? (%s = g_regex_new (\"".printf (cname, cname)
-			+ re + "\", " + flags + ", 0, NULL)) : %s".printf (cname));
+		var regex_const = new CCodeConstant ("_thread_safe_regex_init (&%s, \"".printf (cname) + re + "\", " + flags + ")");
 
 		source_declarations.add_constant_declaration (cdecl);
 		expr.ccodenode = regex_const;
