Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * build command: gcc -pthread -o test test.c; ./test
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <pthread.h>
- #define COUNT (10)
- #define ASCII_LOW_EN (0x61)
- static bool _Global_Func1_Done = false;
- static void _Func1_Print_Num(void)
- {
- int Idx = 0;
- /* Print number 1. */
- for (Idx = 0; Idx < COUNT; Idx++)
- {
- printf("%d\n", Idx + 1);
- }
- /* Set done flag. (or send event to func2) */
- _Global_Func1_Done = true;
- }
- static bool _Global_Func2_Done = false;
- static void _Func2_Print_Char(void)
- {
- int Idx = 0;
- /* Wait func1 done. (or recv event from func1) */
- while (_Global_Func1_Done == false)
- {
- sleep(1);
- }
- /* Print char 'a'. */
- for (Idx = 0; Idx < COUNT; Idx++)
- {
- printf("%c\n", ASCII_LOW_EN + Idx);
- }
- _Global_Func2_Done = true;
- }
- static void _Create_Task(void * pFunc)
- {
- int Ret = 0;
- pthread_t stThread = 0;
- Ret = pthread_create(&stThread, NULL, pFunc, NULL);
- if (Ret != 0)
- {
- printf("pthread_create() fail: return = %d\n", Ret);
- }
- }
- int main(void)
- {
- _Create_Task(_Func2_Print_Char);
- _Create_Task(_Func1_Print_Num);
- /* Wait task done. */
- while ((_Global_Func1_Done == false) ||
- (_Global_Func2_Done == false))
- {
- sleep(1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement