2 个线程并行 linux
2 thread parallel in linux
我刚开始实现线程。我想创建 1 个主线程和 2 个线程 运行ning 并行。
这是我的代码:
#include <stdio.h>
#include <pthread.h>
#include <glib.h>
#include <time.h>
#define THREAD1 1
#define THREAD2 2
GMainLoop *loop1;
GMainLoop *loop2;
pthread_t pth1; // this is our thread identifier
pthread_t pth2; // this is our thread identifier
gboolean timeout_callback1(gpointer data){
clock_t start = clock();
int msec = start * 1000 / CLOCKS_PER_SEC;
printf("timeout_callback ==== 1 at %d seconds %d milliseconds\n", msec/1000, msec%1000);
}
gboolean timeout_callback2(gpointer data){
sleep(2);
clock_t start = clock();
int msec = start * 1000 / CLOCKS_PER_SEC;
printf("timeout_callback ==== 2 at %d seconds %d milliseconds\n", msec/1000, msec%1000);
}
/* This is our thread function. It is like main(), but for a thread */
void *threadFunc(void *arg)
{
int *index;
int i = 0;
index=(int*)arg;
if (index == THREAD1){
printf("threadFunc: %d\n", index);
loop1 = g_main_loop_new ( NULL , FALSE );
//add source to default context
g_timeout_add (100 , timeout_callback1 , loop1);
g_main_loop_run (loop1);
g_main_loop_unref(loop1);
} else {
if (index == THREAD2){
printf("threadFunc: %d\n", index);
loop2 = g_main_loop_new ( NULL , FALSE );
//add source to default context
g_timeout_add (100 , timeout_callback2 , loop2);
g_main_loop_run (loop2);
g_main_loop_unref(loop2);
}else
printf("index not support\n");
}
return NULL;
}
int main(void)
{
int i = 0;
/* Create worker thread */
pthread_create(&pth1,NULL,threadFunc,THREAD1);
pthread_create(&pth2,NULL,threadFunc,THREAD2);
/* wait for our thread to finish before continuing */
while(1)
{
usleep(1);
//printf("main() is running...\n");
//++i;
}
return 0;
}
命令构建:
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include thread.c -o thread -lglib-2.0 -lpthread
结果:
threadFunc: 2
threadFunc: 1
timeout_callback ==== 1 at 0 seconds 2 milliseconds
timeout_callback ==== 2 at 0 seconds 72 milliseconds
timeout_callback ==== 1 at 0 seconds 72 milliseconds
timeout_callback ==== 2 at 0 seconds 142 milliseconds
timeout_callback ==== 1 at 0 seconds 142 milliseconds
timeout_callback ==== 2 at 0 seconds 218 milliseconds
timeout_callback ==== 1 at 0 seconds 218 milliseconds
timeout_callback ==== 2 at 0 seconds 283 milliseconds
timeout_callback ==== 1 at 0 seconds 283 milliseconds
timeout_callback ==== 2 at 0 seconds 348 milliseconds
timeout_callback ==== 1 at 0 seconds 348 milliseconds
timeout_callback ==== 2 at 0 seconds 421 milliseconds
这个结果和我预料的一样。我认为 timeout_callback1 被调用的次数更多 timeout_callback2 因为在函数中 timeout_callback2 有睡眠(2);
你能帮我解释一下结果吗?
请给我一些建议如何 timeout_callback1 运行 独立 timeout_callback2 ?
非常感谢。
如果您在此处阅读 g_timeout_add
文档:
https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-timeout-add 你可以发现:
This internally creates a main loop source using g_timeout_source_new() and attaches it to the global GMainContext
来自这里的 GMainContex:https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#GMainContext
The GMainContext struct is an opaque data type representing a set of sources to be handled in a main loop.
此外:
GMainContext can only be running in a single thread, but sources can
be added to it and removed from it from other threads.
因此这实际上是有道理的,因为 THREAD1
和 THREAD2
将以相同的速率调用 g_timeout_add()
。
我刚开始实现线程。我想创建 1 个主线程和 2 个线程 运行ning 并行。 这是我的代码:
#include <stdio.h>
#include <pthread.h>
#include <glib.h>
#include <time.h>
#define THREAD1 1
#define THREAD2 2
GMainLoop *loop1;
GMainLoop *loop2;
pthread_t pth1; // this is our thread identifier
pthread_t pth2; // this is our thread identifier
gboolean timeout_callback1(gpointer data){
clock_t start = clock();
int msec = start * 1000 / CLOCKS_PER_SEC;
printf("timeout_callback ==== 1 at %d seconds %d milliseconds\n", msec/1000, msec%1000);
}
gboolean timeout_callback2(gpointer data){
sleep(2);
clock_t start = clock();
int msec = start * 1000 / CLOCKS_PER_SEC;
printf("timeout_callback ==== 2 at %d seconds %d milliseconds\n", msec/1000, msec%1000);
}
/* This is our thread function. It is like main(), but for a thread */
void *threadFunc(void *arg)
{
int *index;
int i = 0;
index=(int*)arg;
if (index == THREAD1){
printf("threadFunc: %d\n", index);
loop1 = g_main_loop_new ( NULL , FALSE );
//add source to default context
g_timeout_add (100 , timeout_callback1 , loop1);
g_main_loop_run (loop1);
g_main_loop_unref(loop1);
} else {
if (index == THREAD2){
printf("threadFunc: %d\n", index);
loop2 = g_main_loop_new ( NULL , FALSE );
//add source to default context
g_timeout_add (100 , timeout_callback2 , loop2);
g_main_loop_run (loop2);
g_main_loop_unref(loop2);
}else
printf("index not support\n");
}
return NULL;
}
int main(void)
{
int i = 0;
/* Create worker thread */
pthread_create(&pth1,NULL,threadFunc,THREAD1);
pthread_create(&pth2,NULL,threadFunc,THREAD2);
/* wait for our thread to finish before continuing */
while(1)
{
usleep(1);
//printf("main() is running...\n");
//++i;
}
return 0;
}
命令构建:
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include thread.c -o thread -lglib-2.0 -lpthread
结果:
threadFunc: 2
threadFunc: 1
timeout_callback ==== 1 at 0 seconds 2 milliseconds
timeout_callback ==== 2 at 0 seconds 72 milliseconds
timeout_callback ==== 1 at 0 seconds 72 milliseconds
timeout_callback ==== 2 at 0 seconds 142 milliseconds
timeout_callback ==== 1 at 0 seconds 142 milliseconds
timeout_callback ==== 2 at 0 seconds 218 milliseconds
timeout_callback ==== 1 at 0 seconds 218 milliseconds
timeout_callback ==== 2 at 0 seconds 283 milliseconds
timeout_callback ==== 1 at 0 seconds 283 milliseconds
timeout_callback ==== 2 at 0 seconds 348 milliseconds
timeout_callback ==== 1 at 0 seconds 348 milliseconds
timeout_callback ==== 2 at 0 seconds 421 milliseconds
这个结果和我预料的一样。我认为 timeout_callback1 被调用的次数更多 timeout_callback2 因为在函数中 timeout_callback2 有睡眠(2);
你能帮我解释一下结果吗? 请给我一些建议如何 timeout_callback1 运行 独立 timeout_callback2 ?
非常感谢。
如果您在此处阅读 g_timeout_add
文档:
https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-timeout-add 你可以发现:
This internally creates a main loop source using g_timeout_source_new() and attaches it to the global GMainContext
来自这里的 GMainContex:https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#GMainContext
The GMainContext struct is an opaque data type representing a set of sources to be handled in a main loop.
此外:
GMainContext can only be running in a single thread, but sources can be added to it and removed from it from other threads.
因此这实际上是有道理的,因为 THREAD1
和 THREAD2
将以相同的速率调用 g_timeout_add()
。