使用C Linux上的线程的错误分段违规核心

时间:2017-11-09 15:49:55

标签: c linux multithreading

我有这段代码

Public Class Closure

    Property OriginalCode As String
    Property CompiledCode As String
    Property Errors As ArrayList
    Property Warnings As ArrayList
    Property Statistics As Dictionary(Of String, Object)

    Public Sub New(Input As String, Optional CompliationLevel As String = "SIMPLE_OPTIMIZATIONS")

        Dim _HttpWebRequest As HttpWebRequest
        Dim _Result As StringBuilder
        Dim ClosureWebServiceURL As String = "http://closure-compiler.appspot.com/compile?"
        Dim ClosureWebServicePOSTData As String = "output_format=json&output_info=compiled_code" &
                                                                        "&output_info=warnings" &
                                                                        "&output_info=errors" &
                                                                        "&output_info=statistics" &
                                                                        "&compilation_level=" & CompliationLevel & "" &
                                                                        "&warning_level=default" &
                                                                        "&js_code={0}"


        '// Create the POST data
        Dim Data = String.Format(ClosureWebServicePOSTData, HttpUtility.UrlEncode(Input))

        _Result = New StringBuilder
        _HttpWebRequest = DirectCast(WebRequest.Create(ClosureWebServiceURL), HttpWebRequest)
        _HttpWebRequest.Method = "POST"
        _HttpWebRequest.ContentType = "application/x-www-form-urlencoded"
        '//Set the content length to the length of the data. This might need to change if you're using characters that take more than 256 bytes
        _HttpWebRequest.ContentLength = Data.Length
        '//Write the request stream
        Using SW As New StreamWriter(_HttpWebRequest.GetRequestStream())
            SW.Write(Data)
        End Using

        Try

            Dim response As WebResponse = _HttpWebRequest.GetResponse()

            Using responseStream As Stream = response.GetResponseStream
                Dim encoding As Encoding = System.Text.Encoding.GetEncoding("utf-8")
                Using readStream As New StreamReader(responseStream, encoding)
                    Dim read(256) As Char
                    Dim count As Integer = readStream.Read(read, 0, 256)
                    While count > 0
                        Dim str As New String(read, 0, count)
                        _Result.Append(str)
                        count = readStream.Read(read, 0, 256)
                    End While
                End Using
            End Using

            Dim js As New JavaScriptSerializer
            js.MaxJsonLength = Int32.MaxValue

            Dim d As Dictionary(Of String, Object) = js.Deserialize(Of Dictionary(Of String, Object))(_Result.ToString())
            Me.CompiledCode = d.NullKey("compiledCode")
            Me.Warnings = TryCast(d.NullKey("warnings"), ArrayList)
            Me.Errors = TryCast(d.NullKey("errors"), ArrayList)
            Me.Statistics = TryCast(d.NullKey("statistics"), Dictionary(Of String, Object))

        Catch ex As Exception
            Me.CompiledCode = ""
            If Me.Errors Is Nothing Then
                Dim er As New List(Of String)
                er.Add(ex.ToString())
                Me.Errors = New ArrayList(er)
            Else
                Me.Errors.Add(ex.ToString())
            End If
        End Try

        Me.OriginalCode = Input

    End Sub

End Class

这就是我把它放在一个线程中的方式

void *recorreNumeroH (void *arg){
struct Data *datosH;
datosH = (struct Data *) arg;
pthread_mutex_lock(&lock);
int t = 0, numero = 0 ,primo = -1;
char linea[13];
FILE *entrada;
FILE *salida;
printf("%i   %i  %s\n",datosH->id, datosH->lineasT, datosH->nomT);
entrada = fopen("entrada.txt","r");
rewind(entrada);
fgets(linea,sizeof(linea),entrada);
salida = fopen (datosH -> nomT ,"w"); 
int cont=0; 
while ((fscanf(entrada, "%i", &numero) != EOF) && (cont != datosH -> lineasT)){
    primo = numprimo(numero);
    fprintf(salida, "%i  %i\n",numero,primo);
    cont++;
} 
fclose(salida);
fflush(stdin);
fclose(entrada);
pthread_mutex_unlock(&lock);
return NULL;
}

编辑: 我像你们中的一些人说的那样创建一个结构,重写 recorreNumero 的某些部分,但它一直给我错误分段违规核心

1 个答案:

答案 0 :(得分:1)

要传递给pthread_create的线程函数的必需和必要签名是:

void *threadFuntion(void *arg);

或者,在您的情况下:

void *recorreNumero(void *arg);

因此,pthread_create调用应该如下所示:

pthread_create(&t1,NULL,recorreNumero,‌​args);

'args'需要正确传递[entrada,pro,lineasT]以供线程稍后使用,例如。通过声明具有这些成员的结构,mallocating one,加载值并将struct ADDRESS作为'args'参数传递。线程函数应该转换和取消引用'arg'来恢复那些参数,然后可以在/它终止时释放传递的struct *。