الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

pthreads in C

بدأه Mina Fouad في 13 أكتوبر 2010 · 7 رد · 1,738 مشاهدة · في لغة C و ++C
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم,

بداية احب اوضح اني مبتدأ في البرمجة مع ال C احاول الان تعلم ال pthreads ولكن لا استطيع كلما اجرب اي مثال لا يعمل بسبب مشكلة غريبة بالنسبة لي ك مبرمج جافا...

المشكلةundefined reference to 'pthread_ctreate' and undefined reference to 'pthread_join'

هذا مثال لكود بسيط ولكنه لا يعمل معي......

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message_function( void *ptr );

main()
{
     pthread_t thread1, thread2;
     char *message1 = "Thread 1";
     char *message2 = "Thread 2";
     int  iret1, iret2;

    /* Create independent threads each of which will execute function */

     iret1 = pthread_create(&thread1, NULL, print_message_function, (void*) message1 );
     iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

     /* Wait till threads are complete before main continues. Unless we  */
     /* wait we run the risk of executing an exit which will terminate   */
     /* the process and all threads before the threads have completed.   */

     pthread_join( thread1, NULL);
     pthread_join( thread2, NULL);

     printf("Thread 1 returns: %d\n",iret1);
     printf("Thread 2 returns: %d\n",iret2);
     exit(0);
}

void *print_message_function( void *ptr )
{
     char *message;
     message = (char *) ptr;
     printf("%s \n", message);
}
); }

شكراً جزيلاً

تم تعديل هذه المشاركة بواسطة Mina Fouad في 13 أكتوبر 2010 في 20:24

--

Mina Fouad

Computer & Systems Engineering Dpt.

Faculty of Engineering

Alexandria University

#2

كلمه لا يعمل لا تساعد احد على مساعدتك ,, لابد ان تكون محدد

ماهو الخطأ بالظبط ؟

2
#3

شكراً ع التنبيه اخي الكريم تم التعديل

--

Mina Fouad

Computer & Systems Engineering Dpt.

Faculty of Engineering

Alexandria University

#4

و عليكم السلام,

عزيزي, أنت لم تقم بعمل link للمكتبة. قم باستخدام الطريقة التالية عند ترجمة الأمثلة التي لديك و حاول دائماً أن تكتب كود standard بقدر الإمكان. قمت بتعديل المثال الذي لديك ليصبح أكثر صحة, و طريقة الترجمة و الربط مع المكتبة تلي ذلك:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message_function( void *ptr );

int main()
{
     pthread_t thread1, thread2;
     char *message1 = "Thread 1";
     char *message2 = "Thread 2";
     int  iret1, iret2;

    /* Create independent threads each of which will execute function */

     iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
     iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

     /* Wait till threads are complete before main continues. Unless we  */
     /* wait we run the risk of executing an exit which will terminate   */
     /* the process and all threads before the threads have completed.   */

     pthread_join( thread1, NULL);
     pthread_join( thread2, NULL); 

     printf("Thread 1 returns: %d\n",iret1);
     printf("Thread 2 returns: %d\n",iret2);
     return 0;
}

void *print_message_function( void *ptr )
{
     char *message;
     message = (char *) ptr;
     printf("%s \n", message);
     return NULL;
}

khaled@ubuntu:~/test$ gcc -Wall -std=c89 -lpthread threads.c
khaled@ubuntu:~/test$ ./a.out
Thread 1 
Thread 2 
Thread 1 returns: 0
Thread 2 returns: 0

إضافة إلى ذلك, فالـ standard output stream ليس thread safe بمعنى أنك يجب أن تقوم بعمل synchronization عند الإخراج. لا تقلق حالياً من هذه النقطة, مادمت في طور كتابة الأمثلة البسيطة.

بالتوفيق.

3
#5

تمام...................................

لكني استخدم ال Eclipse ك IDE فماذا افعل لكي استطيع استخدامه بطريقة مباشرة .....

--

Mina Fouad

Computer & Systems Engineering Dpt.

Faculty of Engineering

Alexandria University

#6

لا داعي اخي الكريم..... اخبرني فقط ال IDE الذي تستخدمه.

--

Mina Fouad

Computer & Systems Engineering Dpt.

Faculty of Engineering

Alexandria University

#7

gedit :) اليس كذلك.

--

Mina Fouad

Computer & Systems Engineering Dpt.

Faculty of Engineering

Alexandria University

#8

فقط للاستفادة من الموضوع ساسرد لكم كيفية تعديل ال IDE Eclipse ليقوم بقراءة وترجمة ال pthreads

نقوم بفتح ال Eclipse ثم من خلال ال properties الخاصة بالمشروع نختار من قائمة ال C/C++ Build نختار settings ثم نختار GCC C Linker ثم نعدل ال command من gcc الي gcc -lpthread

فقط.......:)

1

--

Mina Fouad

Computer & Systems Engineering Dpt.

Faculty of Engineering

Alexandria University

مواضيع مشابهة