Write a multithreaded program C, hurry, please help. Simple would be fine.

Updated on technology 2024-03-21
17 answers
  1. Anonymous users2024-02-07

    #pragma once

    #include

    handle threadhandle;

    dword winapi threadproc( lpvoid lpparameter )

    int ia,ib;

    ia = 5;

    ib = 6;

    int isum = ia+ib;

    Terminate the thread. terminatethread( threadhandle, 0 );

    return 0;

    int main()

    Create a thread (when a thread is created, the thread function is automatically called).

    ThreadHandle = CreateThread( null, usually null

    0, usually 0

    threadproc, a thread function (automatically called).

    null, generally null

    0, usually 0

    null //

    system( "pause" );

  2. Anonymous users2024-02-06

    Simple multi-threaded programming.

    Multithreading under Linux follows the Posix thread interface, which is called pthread. To write a multi-threaded program under Linux, you need to use header files, and you need to use libraries when connecting. By the way, the implementation of pthread under Linux is implemented by the system call clone().

    clone() is a Linux-specific system call, it is used in a fork-like way, for more information about clone(), interested readers can check the relevant documentation. Below we show one of the simplest multithreaded programs.

    #include

    #include

    void thread(void)

    int i;

    for(i=0;i<3;i++)

    printf("this is a pthread.");

    int main(void)

    pthread_t id;

    int i,ret;

    if(ret!=0){

    printf ("create pthread error!");

    exit (1);

    for(i=0;i<3;i++)

    printf("this is the main process.");

    pthread_join(id,null);

    return (0);

    We compile this program:

    gcc -lpthread -o example1 and we get the following result:

    this is the main process.

    this is a pthread.

    this is the main process.

    this is the main process.

    this is a pthread.

    this is a pthread.

    Running it again, we might get something like this:

    this is a pthread.

    this is the main process.

    this is a pthread.

    this is the main process.

    this is a pthread.

    this is the main process.Forget.

  3. Anonymous users2024-02-05

    1. Click the "Project" tab in the menu bar, and the last item in the drop-down list is "Project Options.".It is to set the properties of the current project.

    2. Select the "Compiler" tab in the pop-up dialog box.

    3. Change the selection of "Runtime Library" to "Multithreaded (Lib)".

    4. You will see that some changes have been made in the text box at the bottom of the dialog box, and the "-mt" option has been added, which is consistent with the solution given by the error message reported by the compiler at the beginning.

    5. After the page is set up, when the source code is compiled, you can happily see that the compilation is completely successful.

  4. Anonymous users2024-02-04

    1. Use the pthread library.

    Perform multi-threading, this is a thread library under linux windows should have its own api, but this kind of thing is generally based on linux. pthread create() to create a thread, pass in the function pointer of fun().

    2. Example: include

    #include

    #include

    #include

    #define max 10

    pthread_t thread[2];

    pthread_mutex_t mut;

    int number=0, i;

    void *thread1()

    printf("thread1: Is the main function waiting for me to complete the task? ");

    pthread_exit(null);

    void *thread2()

    printf("thread2: Is the main function waiting for me to complete the task? ");

    pthread_exit(null);

    void thread_create(void)void thread_wait(void)if(thread[1] !=0)

    int main()

  5. Anonymous users2024-02-03

    C11 comes with a multi-threaded library, while the old version of C needs to call the corresponding system API or third-party library.

  6. Anonymous users2024-02-02

    1。If a program is running for too long and the interface dies, you can put it into a thread.

    2。Some fragments need to be executed repeatedly and can be put into threads.

    3。Improve the operational efficiency of the program.

  7. Anonymous users2024-02-01

    Just want to do different things at the same time.

  8. Anonymous users2024-01-31

    The execution of threads needs to provide computing resources, and the most direct computing resources of the computer, in addition to memory, is the CPU. The execution of threads takes up CPU time. The operating system is designed to emulate the user's "look of concurrency.""The underlying layer is actually the strategy of using time slice polling, that is, the CPU time will be cut into smaller granular time slices, and then submitted to each thread for execution in turn, and each thread will get the opportunity to run within the user's perceptible time.

    Leave it to "individual threads" to execute, or more precisely, to "individual threads waiting to run". The suspend thread, in fact, is to tell the operating system that this thread is inactive, and there is no need to operate for the time being, which can be removed from the above-mentioned queue of threads waiting to run, and then put into the "suspend thread pool", and when the operating system cpu is scheduled in the future, it will no longer poll this thread to waste CPU time needlessly, so that other really active threads can get more CPU running time.

    So what if the thread hangs and wants to re-run it at some point in the future? The process of waking up the thread is actually from the "Suspend Thread Pool" to the "Queue of Threads Waiting to Run", when the CPU time polls this thread, you can play happily again.

  9. Anonymous users2024-01-30

    Generally speaking, it is to reduce the CPU usage of threads.

  10. Anonymous users2024-01-29

    If two threads can run at the same time, the performance can be increased by half, and here is a prerequisite: your program is running with two CPU cores allocated and there are no unexpected interruptions during execution. In the actual environment, the number of processes and threads in the operating system is always more than the number of CPU cores, and you cannot guarantee that the program will always get the right number of CPUs for each run and that the execution process will not be interrupted.

    For C++03 98, because the language itself does not directly support multithreading, it can only use third-party thread libraries or directly use operating system APIs, and some third-party thread libraries do not provide sufficient concurrency capacity, and the performance of threads is low due to kernel mode switching.

  11. Anonymous users2024-01-28

    Multi-threaded and single-threaded execution efficiency issues. It is not just a matter of looking at one angle, but also about other aspects. For example, it depends on the problems and scenarios in actual development, and even depends on the hardware level (single-core or multi-core) and software level (multi-threaded implementation principle).

  12. Anonymous users2024-01-27

    Yes, theoretically two threads working at the same time are nearly twice as efficient as one thread.

    For example, if you have written UI, you should often use multi-threaded programming to prevent interface freeze; If you've ever done network programming, you'll find that the main purpose of multithreading is to listen to and receive messages. The main purpose of multithreading in these places is not to be efficient.

  13. Anonymous users2024-01-26

    Multithreaded work can increase the speed of problem solving.

    You can give an example of multithreading, but more people call it distributed computing multithreading, which is mainly to create multiple threads, each thread is responsible for its own business, and each thread is only responsible for the main thread.

    As for the speed of solving the problem, it should be possible to increase it by half, but you should pay attention to the mutual exclusion of resources, and if the mutual exclusion is not handled well, it is estimated that it will be half the effort.

  14. Anonymous users2024-01-25

    Yes, an important application of multithreading is parallel computing.

  15. Anonymous users2024-01-24

    When multiple threads access an exclusive shared resource, you can use a Critical Zone object. Only one thread can have a critical area object at any one time, the thread with the critical area can access the protected resource or segment, and the other threads that want to enter the critical area will be suspended and wait until the thread with the critical area gives up the critical area, so as to ensure that no multiple threads will access the shared resources at the same time.

    The usage of the ccriticalSection class is very simple, and the steps are as follows:

    Define a global object of the CcriticalSection class (so that it is accessible to all threads), such as CcriticalSection Critical Section;

    Before accessing the resource that needs to be protected or **, call lock(), a member of the ccriticalSection class, to obtain a critical section object: critical;

    This function is called during the procedure to cause the thread to obtain the critical zone it requested. If no other thread occupies the critical area object at this time, the thread that calls lock() obtains the critical area; Otherwise, the thread is suspended and placed in a system queue until the thread that currently owns the critical area releases the critical area.

    After accessing the critical section, use the member function unlock() of the ccriticalsection to release the critical section: critical;

    In layman's terms, thread A executes to critical; statement, if other threads (b) are executing critical; critical section unlock();Thread A waits until Thread B finishes executing the critical section unlock();statement, thread A will continue to execute.

  16. Anonymous users2024-01-23

    All threads in the process share the virtual address space of the process, the threads in the process are executed in parallel, and the system divides the execution time for each thread

  17. Anonymous users2024-01-22

    Take a look at our operating system book, it's up.

Related questions
7 answers2024-03-21

I'll fix it, friend, I don't want to divide it, hehe.

r=0; c=1;It is to assign an initial value to r and c. >>>More

10 answers2024-03-21

arp packages can be used in ms's platformsdk in ip helper dword sendarp (ipaddr destip, ipaddr srcip, pulong pmacaddr, pulong phyaddrlen); >>>More

5 answers2024-03-21

The ** provided is mainly based on the following two errors: >>>More