c语言线程设计,c++线程编程

发布时间:2022-11-23

本文目录一览:

  1. c语言多线程编程问题
  2. C语言多线程实现
  3. 用C语言写多线程程序
  4. C语言如何创建线程(windows)系统中
  5. c语言怎么创建线程和使用?
  6. 用C语言在windows或者Linux上面,编写一个多线程程序

c语言多线程编程问题

C语言中多线程的实现原理就是线程的原理,本人只了解Linux下面的C,Linux下面的线程,不就是进程中的一个控制流么,相对来说代码很简单,但是原理却是很复杂,很难说清,还需要自己详细学习研究,下面是一个很简单的例子,哪边都能找到,自己运行下看看吧

#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10
pthread_t thread[2];
pthread_mutex_t mut;
int number=0, i;
void *thread1()
{
    printf ("thread1 : I'm thread 1\n");
    for (i = 0; i < MAX; i++)
    {
        printf("thread1 : number = %d\n",number);
        pthread_mutex_lock(&mut);
        number++;
        pthread_mutex_unlock(&mut);
        sleep(2);
    }
    printf("thread1 :主函数在等我完成任务吗?\n");
    pthread_exit(NULL);
}
void *thread2()
{
    printf("thread2 : I'm thread 2\n");
    for (i = 0; i < MAX; i++)
    {
        printf("thread2 : number = %d\n",number);
        pthread_mutex_lock(&mut);
        number++;
        pthread_mutex_unlock(&mut);
        sleep(3);
    }
    printf("thread2 :主函数在等我完成任务吗?\n");
    pthread_exit(NULL);
}
void thread_create(void)
{
    int temp;
    memset(thread, 0, sizeof(thread)); // comment1
    /*创建线程*/
    if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) // comment2
        printf("线程1创建失败!\n");
    else
        printf("线程1被创建\n");
    if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) // comment3
        printf("线程2创建失败");
    else
        printf("线程2被创建\n");
}
void thread_wait(void)
{
    /*等待线程结束*/
    if(thread[0] !=0) { // comment4
        pthread_join(thread[0],NULL);
        printf("线程1已经结束\n");
    }
    if(thread[1] !=0) { // comment5
        pthread_join(thread[1],NULL);
        printf("线程2已经结束\n");
    }
}
int main()
{
    /*用默认属性初始化互斥锁*/
    pthread_mutex_init(&mut,NULL);
    printf("我是主函数哦,我正在创建线程,呵呵\n");
    thread_create();
    printf("我是主函数哦,我正在等待线程完成任务阿,呵呵\n");
    thread_wait();
    return 0;
}

C语言多线程实现

多线程随机选号程序 以下程序运行后看起来比较有意思,像一个随机选号程序,但不是完全按照问题所说的写的,可供参考,要改很容易。

// 多线程随机选号程序示例
#include <stdio.h>
#include <windows.h>
#include <ctime>
#include <cstdlib>
#include <process.h>
bool g_run = true; // 是否运行
void userInput(void*) // 监视输入的线程函数
{
    while (true)
    {
        if (getchar() == '\n') // 是否输入回车
        {
            g_run = !g_run; // 回车运行 / 回车暂停
        }
        Sleep(10); // 延迟
    }
}
int main()
{
    srand(time(0)); // 随机数种子
    _beginthread(userInput, 0, NULL); // 开线程
    while (true)
    {
        if (g_run)
        {
            system("cls"); // 清屏
            int t = rand() % 1000 + 1; // 1-1000的随机数
            printf("\n%d", t); // 输出
        }
        Sleep(50); // 延迟50毫秒
    }
    return 0;
}

用C语言写多线程程序

thread_creation.c
gcc thread_creation.c
会在当前目录下生成可执行的a.out文件
./a.out

C语言如何创建线程(windows)系统中

下面为C语言调用WIN API实现创建线程:

  1. 导入 windows.h 头文件
  2. 声明实现方法 DWORD WINAPI ThreadProc1( LPVOID lpParam ) {}
  3. main() 方法中调用 CreateThread(NULL, 0, ThreadProc1, NULL, 0, NULL);
    要注意的是主线程不能结束,如果主线程结束,则它的子线程也会被杀死。
#include <windows.h>
#include <stdio.h>
#include <time.h>
DWORD WINAPI ThreadProc1( LPVOID lpParam )
{
    int i=0;
    time_t timer;
    while(1)
    {
        timer=time(NULL);
        printf("The current time is: %s\n",asctime(localtime(&timer)));
        Sleep(1000);
    }
}
void main()
{
    int i=0;
    // 让主线程进入循环,主线程若退出,子线程1,2会被系统“杀死”
    // 创建线程1
    CreateThread(
        NULL, // default security attributes
        0, // use default stack size
        ThreadProc1, // thread function
        NULL, // argument to thread function
        0, // use default creation flags
        NULL); // returns the thread identifier
    for(;;)
    {
        ;
    }
}

c语言怎么创建线程和使用?

进程的生命周期:

  1. 创建 --- fork
  2. 执行 --- a. exec
    b. 子进程实现代码逻辑
  3. 结束 --- exit_exit
    僵尸态进程 --- waitwaitpid
    孤儿进程

进程存在的问题:

  1. 进程的创建 --- 复制(时间和空间的开销很大)
  2. 进程的运行 --- 调度
    pthread_create 创建一个线程,thread 是用来表明创建线程的ID,attr 指出线程创建时候的属性,我们用 NULL 来表明使用缺省属性。start_routine 函数指针是线程创建成功后开始执行的函数,arg 是这个函数的唯一一个参数,表明传递给 start_routine 的参数。 pthread_exit 函数和 exit 函数类似用来退出线程。这个函数结束线程,释放函数的资源,并在最后阻塞,直到其他线程使用 pthread_join 函数等待它。然后将 *retval 的值传递给 **thread_return。由于这个函数释放所有的函数资源,所以 retval 不能够指向函数的局部变量。 pthread_joinwait 调用一样用来等待指定的线程。下面我们使用一个实例来解释一下使用方法。在实践中,我们经常要备份一些文件。下面这个程序可以实现当前目录下的所有文件备份。

参考资料

CSDN [引用时间2018-1-9]

用C语言在windows或者Linux上面,编写一个多线程程序

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
    int *pt = (int*)lpParam;
    printf("I am thread %d\r\n", *pt);
}
int main()
{
    const int Count = 4;
    int datas[Count];
    DWORD dwThreadId[Count];
    HANDLE hThread[Count];
    int i;
    for(i = 0; i < Count; i++)
    {
        datas[i] = i + 1;
        hThread[i] = CreateThread(NULL, 0, ThreadProc, &datas[i], 0, &dwThreadId[i]);
    }
    WaitForMultipleObjects(Count, hThread, TRUE, INFINITE);
    for(i = 0; i < Count; i++)
    {
        CloseHandle(hThread[i]);
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}